package com.eeepay.v2_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eeepay.v2_library.R;

/**
 * Created by Ching on 2016/3/3.
 */
public class HorizontalItemView extends RelativeLayout implements View.OnClickListener {

    private TextView leftTv, rightTv, iconLeft, iconRight;
    private Drawable right_drawable, left_drawable;
    private Context mContext;
    private String leftText, rightText;
    private View view;
    private boolean showLeftDraw, showRightDraw, showLeftText, showRightText;
    private int rightTextColor;
    private int leftTextColor;
    private LinearLayout rl_itemview;
    public HorizontalItemView(Context context) {
        this(context, null);
    }

    public HorizontalItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HorizontalItemView);
        leftText = typedArray.getString(R.styleable.HorizontalItemView_leftText);
        rightText = typedArray.getString(R.styleable.HorizontalItemView_rightText);
        showLeftDraw = typedArray.getBoolean(R.styleable.HorizontalItemView_showLeftDraw, true);
        showRightDraw = typedArray.getBoolean(R.styleable.HorizontalItemView_showRightDraw, true);
        showLeftText = typedArray.getBoolean(R.styleable.HorizontalItemView_showLeftText, true);
        showRightText = typedArray.getBoolean(R.styleable.HorizontalItemView_showRightText, true);

        leftTextColor = typedArray.getColor(R.styleable.HorizontalItemView_leftTextColor, getResources().getColor(R.color.unify_txt_color_gray));
        rightTextColor = typedArray.getColor(R.styleable.HorizontalItemView_rightTextColor, getResources().getColor(R.color.unify_txt_color_black));

        int rightDrawable_id = typedArray.getResourceId(R.styleable.HorizontalItemView_iconRight, R.drawable.btn_right_arrow);
        right_drawable = context.getResources().getDrawable(rightDrawable_id);

        int leftDrawable_id = typedArray.getResourceId(R.styleable.HorizontalItemView_iconLeft, -1);
        if (leftDrawable_id != -1) {
            left_drawable = context.getResources().getDrawable(leftDrawable_id);
        }

        view = LayoutInflater.from(context).inflate(R.layout.view_horizontal_itemview, this, true);
        leftTv = (TextView) view.findViewById(R.id.tv_hiv_leftText);
        rightTv = (TextView) view.findViewById(R.id.tv_hiv_rightText);
        iconLeft = (TextView) view.findViewById(R.id.tv_hiv_iconLeft);
        iconRight = (TextView) view.findViewById(R.id.tv_hiv_iconRight);

        rl_itemview = (LinearLayout)view.findViewById(R.id.rl_itemview);

        setViewContent();
        typedArray.recycle();

    }

    /**
     * 设置控件内容
     */
    private void setViewContent() {

        leftTv.setVisibility(showLeftText ? View.VISIBLE : View.GONE);
        rightTv.setVisibility(showRightText ? View.VISIBLE : View.GONE);
        iconLeft.setVisibility(showLeftDraw ? View.VISIBLE : View.GONE);
        iconRight.setVisibility(showRightDraw ? View.VISIBLE : View.GONE);

        if (leftText != null) {
            leftTv.setText(leftText);
        }
        if (right_drawable != null) {
            right_drawable.setBounds(0, 0, right_drawable.getMinimumWidth(), right_drawable.getMinimumHeight());
            iconRight.setCompoundDrawables(null, null, right_drawable, null);
        }

        if (left_drawable != null) {
            left_drawable.setBounds(0, 0, left_drawable.getMinimumWidth(), left_drawable.getMinimumHeight());
            iconLeft.setCompoundDrawables(left_drawable, null, null, null);
        }
        if (rightText != null) {
            rightTv.setText(rightText);
        }

        if (rightTextColor != -1) {
            rightTv.setTextColor(rightTextColor);
        }
        if (leftTextColor != -1) {
            leftTv.setTextColor(leftTextColor);
        }

    }

    public void setRightBackgroundResource(int resid) {
        rightTv.setBackgroundResource(resid);
    }

    public void setRightResource(int resid) {
        Drawable drawable = mContext.getResources().getDrawable(resid);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        rightTv.setCompoundDrawables(drawable, null, null, null);
    }

    /**
     * @param color 设置右边文本颜色
     */
    public void setRightTextColor(int color) {
        if (rightTv != null) {
            rightTv.setTextColor(color);
        }
    }

    public void setRootBackColor(int color)
    {
        if (rl_itemview!=null)
        {
            rl_itemview.setBackgroundColor(getResources().getColor(color));
        }
    }

    /**
     * 设置右边文本
     */
    public void setRightText(String rightContent) {
        if (rightTv != null) {
            rightTv.setText(rightContent);
        }
    }

    public void showRightDraw(boolean showRightDraw) {
        iconRight.setVisibility(showRightDraw ? View.VISIBLE : View.GONE);
    }

    /**
     * 是否显示右边文本
     */
    public void showRightText(int visibility) {

        if (rightTv != null) {
            rightTv.setVisibility(visibility);
        }
    }

    public TextView getRightTv() {
        return rightTv;
    }

    public String getRightText() {
        return rightTv.getText().toString().trim();
    }

    @Override
    public void onClick(View v) {

    }

    public void setLeftText(String leftContent){
        leftTv.setText(leftContent);
    }


}